package com.liugui;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int[] ary = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};//要分割的数组
        int splitSize = 5;//分割的块大小
        Object[] subAry = splitAry(ary, splitSize);//分割后的子块数组
        for (Object obj : subAry) {//打印输出结果
            int[] aryItem = (int[]) obj;
            for (int i = 0; i < aryItem.length; i++) {
                System.out.print(aryItem[i] + ", ");
            }
            System.out.println();
        }
    }

    /**
     * splitAry方法
     *
     * @param ary     要分割的数组
     * @param subSize 分割的块大小
     * @return
     */

    private static Object[] splitAry(int[] ary, int subSize) {

        int count = ary.length % subSize == 0 ? ary.length / subSize : ary.length / subSize + 1;
        List subAryList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int index = i * subSize;
            List list = new ArrayList();
            int j = 0;
            while (j < subSize && index < ary.length) {
                list.add(ary[index++]);
                j++;
            }
            subAryList.add(list);
        }

        Object[] subAry = new Object[subAryList.size()];
        for (int i = 0; i < subAryList.size(); i++) {
            List<Integer> subList = (List<Integer>) subAryList.get(i);
            int[] subAryItem = new int[subList.size()];
            for (int j = 0; j < subList.size(); j++) {
                subAryItem[j] = subList.get(j).intValue();
            }
            subAry[i] = subAryItem;
        }
        return subAry;
    }
}
